package com.zl.tcbnewcar.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.ui.fragment.MyServiceListFragment;
import com.zl.tcbnewcar.utils.FragmentHelperReform;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyServiceActivity extends BaseActivity {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_share)
    ImageView ivTitleShare;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.my_service_tab)
    TabLayout myServiceTab;
    @BindView(R.id.my_service_rl)
    RelativeLayout myServiceRl;
    private MyServiceListFragment fragment0;
    private MyServiceListFragment fragment1;
    private MyServiceListFragment fragment2;
    private MyServiceListFragment fragment3;
    private MyServiceListFragment fragment4;
    private FragmentHelperReform helper;
    private boolean isEdit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_service);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {
        myServiceTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        helper.showFragment(fragment0);
                        break;
                    case 1:
                        helper.showFragment(fragment1);
                        break;
                    case 2:
                        helper.showFragment(fragment2);
                        break;
                    case 3:
                        helper.showFragment(fragment3);
                        break;
                    case 4:
                        helper.showFragment(fragment4);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initView() {
        myServiceTab.addTab(myServiceTab.newTab().setText("处理违章"));
        myServiceTab.addTab(myServiceTab.newTab().setText("维修保养"));
        myServiceTab.addTab(myServiceTab.newTab().setText("保险服务"));
        myServiceTab.addTab(myServiceTab.newTab().setText("年审服务"));
        myServiceTab.addTab(myServiceTab.newTab().setText("金融信贷"));

        fragment0 = MyServiceListFragment.newInstance(1);
        fragment1 = MyServiceListFragment.newInstance(2);
        fragment2 = MyServiceListFragment.newInstance(3);
        fragment3 = MyServiceListFragment.newInstance(4);
        fragment4 = MyServiceListFragment.newInstance(5);
        helper = FragmentHelperReform.builder(mActivity).attach(R.id.my_service_rl)
                .addFragment(fragment0)
                .addFragment(fragment1)
                .addFragment(fragment2)
                .addFragment(fragment3)
                .addFragment(fragment4)
                .commit();
        tvTitleName.setText("我的服务");

        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText("编辑");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(mActivity).onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.iv_title_back, R.id.tv_title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_right:
                edit();
                break;
        }
    }

    private void edit() {
        if (isEdit) {
            isEdit = false;
            tvTitleRight.setText("编辑");
        } else {
            isEdit = true;
            tvTitleRight.setText("取消编辑");
        }
        fragment0.isEdit(isEdit);
        fragment1.isEdit(isEdit);
        fragment2.isEdit(isEdit);
        fragment3.isEdit(isEdit);
        fragment4.isEdit(isEdit);
    }
}
